import pandas as pd
from datetime import datetime
from libs.readcsv import allData
from pandas.core.frame import DataFrame


# 计算时间范围内的各省份请求总数
def requestNum(start_time=None, end_time=None):
    if start_time is not None and end_time is not None:
        start_time = (start_time.strftime("%Y-%m-%d %H:%M:%S")).split()[0] + " 00:00:00"
        end_time = (end_time.strftime("%Y-%m-%d %H:%M:%S")).split()[0] + " 00:00:00"
        start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
        end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
    initdata = allData()  # 读取数据
    if start_time is None and start_time is None:
        province = initdata.pivot_table(index=['remote_ip_province'], aggfunc='size')
        province = province.to_frame(name='num').reset_index()
    else:
        initdata['time'] = initdata['time'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        data = initdata[(initdata['time'] >= start_time) & (initdata['time'] <= end_time)]
        province = data.pivot_table(index=['remote_ip_province'], aggfunc='size')
        province = province.to_frame(name='num').reset_index()
    return province


def totalSize(start_time=None, end_time=None):
    if start_time is not None and end_time is not None:
        start_time = (start_time.strftime("%Y-%m-%d %H:%M:%S")).split()[0] + " 00:00:00"
        end_time = (end_time.strftime("%Y-%m-%d %H:%M:%S")).split()[0] + " 00:00:00"
        start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
        end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
    initdata = allData()  # 读取数据
    if start_time is None and start_time is None:
        total_size = initdata.groupby(['remote_ip_province'])['object_size'].sum().reset_index()
    else:
        initdata['time'] = initdata['time'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        data = initdata[(initdata['time'] >= start_time) & (initdata['time'] <= end_time)]
        total_size = data.groupby(['remote_ip_province'])['object_size'].sum().reset_index()
    df1 = pd.core.frame.DataFrame(total_size.values)
    df1.rename(columns={0: 'remote_ip_province', 1: 'object_size'}, inplace=True)
    return df1


def totalTime(start_time=None, end_time=None):
    initdata = allData()  # 读取数据
    if start_time is None and start_time is None:
        total_time = initdata.groupby(['remote_ip_province'])['total_time'].sum().reset_index()
        # print(total_time)
    else:
        start_time = (start_time.strftime("%Y-%m-%d %H:%M:%S")).split()[0] + " 00:00:00"
        end_time = (end_time.strftime("%Y-%m-%d %H:%M:%S")).split()[0] + " 00:00:00"
        start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
        end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

        initdata['time'] = initdata['time'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        data = initdata[(initdata['time'] >= start_time) & (initdata['time'] <= end_time)]
        total_time = data.groupby(['remote_ip_province'])['total_time'].sum().reset_index()
        # print(total_time)
    df1 = pd.core.frame.DataFrame(total_time.values)
    df1.rename(columns={0: 'remote_ip_province', 1: 'object_time'}, inplace=True)
    return df1
